package com.ywy.job.controller;

import com.ywy.job.enums.JobStatusEnum;
import com.ywy.job.model.SysJob;
import com.ywy.job.rest.PageParam;
import com.ywy.job.rest.PageResult;
import com.ywy.job.rest.Result;
import com.ywy.job.service.SysJobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * 定时任务Controller
 *
 * @author ywy
 * @date 2021/11/3 18:19
 */
@Controller
@RequestMapping("job")
public class SysJobController {
    @Autowired
    private SysJobService jobService;

    /**
     * 任务列表页面
     * @return
     */
    @GetMapping("")
    public String job() {
        return "job";
    }

    /**
     * 任务编辑页面
     * @return
     */
    @GetMapping("edit")
    public String edit() {
        return "job-edit";
    }

    /**
     * 查询任务列表数据
     * @param param
     * @return
     */
    @ResponseBody
    @GetMapping("list")
    public Result list(PageParam param) {
        PageResult pageResult = jobService.listPage(param);
        return Result.success("查询成功", pageResult);
    }
    /**
     * 保存任务数据
     * @param job
     * @return
     */
    @ResponseBody
    @PostMapping("save")
    public Result save(SysJob job) {
        int flag = jobService.save(job);
        if (flag > 0) {
            return Result.success();
        } else if (flag == -1) {
            return Result.failure("修改之前请先停止任务！");
        }
        return Result.failure("保存任务失败！");
    }

    /**
     * 立即执行任务
     * @param id
     * @return
     */
    @ResponseBody
    @PostMapping("run/{id}")
    public Result run(@PathVariable("id") Long id) {
        int flag = jobService.run(id);
        if(flag > 0) {
            return Result.success();
        } else if (flag == -1) {
            return Result.failure("立即执行请先开启任务！");
        }
        return Result.failure("立即执行任务失败！");
    }

    /**
     * 修改任务状态
     * @param id
     * @param jobStatus
     * @return
     */
    @ResponseBody
    @PostMapping("/changeStatus/{id}")
    public Result changeStatus(@PathVariable("id") Long id, Boolean jobStatus) {
        Integer status = jobStatus ? JobStatusEnum.RUNNING.getCode() : JobStatusEnum.STOP.getCode();
        if (jobService.changeStatus(id, status)) {
            return Result.success();
        }
        return Result.failure("任务状态修改失败");
    }

    /**
     * 删除任务
     * @param id
     * @return
     */
    @ResponseBody
    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Long id) {
        int flag = jobService.delete(id);
        if (flag > 0) {
            return Result.success();
        } else if (flag == -1) {
            return Result.failure("删除前请先停止任务！");
        }
        return Result.failure("删除任务失败！");
    }

    /**
     * 批量删除任务
     */
    @ResponseBody
    @PostMapping("/batchDelete")
    public Result batchDelete(@RequestParam("ids[]") Long[] ids) {
        int flag = jobService.batchDelete(ids);
        if (flag > 0) {
            return Result.success();
        } else if (flag == -1) {
            return Result.failure("删除前请先停止任务！");
        }
        return Result.failure("删除任务失败！");
    }
}
